package com.zys.el.search.service;


import com.zys.el.search.dao.AnticleDao;
import com.zys.el.search.po.Anticle;
import com.zys.el.search.po.SearchRequest;
import com.zys.el.search.po.SearchResult;
import com.zys.el.user.config.po.New;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class SearchService {

    @Autowired
    private AnticleDao anticleDao;

    public SearchResult search(SearchRequest searchRequest) throws IOException {
        String key = searchRequest.getKey();
        if (key == null) {
            return null;
        }
        List<Anticle> all = anticleDao.search("all", searchRequest);
        List<New> list = new ArrayList<>();
        for (Anticle anticle : all) {
            list.add(New.builder()
                    .user_name(anticle.getUserName())
                    .user_image(anticle.getUserImage())
                    .title(anticle.getItem())
                    .id(anticle.getNoteId())
                    .content(anticle.getIntroduction()).build());
        }
        SearchResult news = SearchResult.builder().News(list).build();

        return news;
    }
}
